zz_

test/clamp.test.js

zzmq.dev@gmail.com[dev1]/mathkit[add-isperfectsquare-n-function-tests] / test / clamp.test.js @ 38505f3e Download
Ladder Summary
  • file Implements a Node.js test suite that verifies `clamp` returns the original value when within bounds, the appropriate bound when the value is out of range, and `undefined` for any non‑numeric or non‑finite inputs.
import { test } from "node:test";
import assert from "node:assert/strict";
import { clamp } from "../src/clamp.js";

test("clamp returns the same value when within bounds", () => {
  assert.strictEqual(clamp(5, 0, 10), 5);
  assert.strictEqual(clamp(0, -5, 5), 0);
});

test("clamp returns lower bound when value is below it", () => {
  assert.strictEqual(clamp(-10, -5, 5), -5);
  assert.strictEqual(clamp(2, 3, 8), 3);
});

test("clamp returns upper bound when value is above it", () => {
  assert.strictEqual(clamp(20, 0, 10), 10);
  assert.strictEqual(clamp(9, 0, 5), 5);
});

test("clamp returns undefined for non‑numeric inputs", () => {
  assert.strictEqual(clamp("a", 0, 1), undefined);
  assert.strictEqual(clamp(0, "b", 1), undefined);
  assert.strictEqual(clamp(0, 0, "c"), undefined);
  assert.strictEqual(clamp(NaN, 0, 1), undefined);
  assert.strictEqual(clamp(0, Infinity, 1), undefined);
});